package com.binhanh.common;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.binhanh.common.constant.Constants;

public class Utils {

	public static String extractYoutubeId(String url) {
		String id = null;
		try {
			String query = new URL(url).getQuery();
			if (query != null) {
				String[] param = query.split("&");
				for (String row : param) {
					String[] param1 = row.split("=");
					if (param1[0].equals("v")) {
						id = param1[1];
					}
				}
			} else {
				if (url.contains("embed")) {
					id = url.substring(url.lastIndexOf("/") + 1);
				}
			}
		} catch (Exception ex) {
		}
		return id;
	}
	
	public static Date convertStringToDate(String datetimeStr){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT_PATTERN);
        try
        {
            return simpleDateFormat.parse(datetimeStr);
        }
        catch (Exception ex)
        {
            System.out.println("Exception "+ex);
        }
        
        return null;
	}

}
